package com.zd.common.filter;

import com.zd.model.Teacher;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SessionTimeOutFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest re=(HttpServletRequest)req;
        HttpServletResponse rp=(HttpServletResponse)resp;
        System.out.println(re.getRequestURI());
        System.out.println(re.getRequestURL());
        if (re.getRequestURI().contains("login")){
            chain.doFilter(re, rp);
        }else{
            Teacher teacher=(Teacher) re.getSession().getAttribute("userInfo");
            //校验session
            if(teacher==null){
//                re.getRequestDispatcher("login.jsp").forward(re,rp);
                rp.sendRedirect("http://localhost:8080/login.jsp");
                return;
            }else{
                //已登录的情况
                chain.doFilter(req,resp);
            }
        }
    }
    public void init(FilterConfig config) throws ServletException {



    }
}
